import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jetty.server.Authentication.User;
import org.eclipse.jetty.util.StringUtil;

import com.jfinal.core.Controller;
import com.jfinal.ext.render.CaptchaRender;
import com.jfinal.validate.Validator;

public class LoginPwd extends Validator {
	private Map<String, String> responseM = new HashMap<String, String>();

	@Override
	protected void validate(Controller c) {
		// 验证码忽略大小写，这里用到了jfinal 自带的验证验证码的方法
		boolean b = CaptchaRender.validate(c, c.getPara("imgcode")
				.toUpperCase(), CommonController.KEY);
		if (!b) {
			addError("imgcodeMsg", "验证码不正确！");
			return;
		}
		// 验证码正确，验证密码
		String phone = c.getPara("phone");

		addError("passwordMsg", "密码不正确！");
		return;
	}

	@Override
	protected void handleError(Controller c) {
		Enumeration<String> en = c.getParaNames();
		while (en.hasMoreElements()) {
			// 错误键，我们规定，所有的错误为 请求字段加上 Msg
			String key = en.nextElement().toString() + "Msg";
			if (c.getAttrForStr(key) != null) {
				responseM.put(key, c.getAttrForStr(key));
			}

		}
		responseM.put("state", "error");
		c.renderJson(responseM);
	}

}